﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Data;
using Microsoft.SharePoint.Administration;
using System.Web.UI.WebControls;

namespace zevenseas.ClassicTheme
{
    public partial class Settings : LayoutsPageBase
    {
        private DataView themesDataView;
        private DataView blockedThemesDataView;
        private List<string> filePaths;

        protected LinkButton btnBlock;
        protected LinkButton btnUnblock;

        //protected Microsoft.SharePoint.WebControls.ToolBarButton Allow;
        //protected Microsoft.SharePoint.WebControls.ToolBarButton Disallow;

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            GetAllThemes(ref themesDataView, ref blockedThemesDataView);

            gridView.DataSource = themesDataView;
            gridView.DataBind();

            gridViewBlocked.DataSource = blockedThemesDataView;
            gridViewBlocked.DataBind();
            base.Render(writer);
        }

        protected override void CreateChildControls()
        {
            btnBlock.Click += new EventHandler(btnBlock_Click);
            btnUnblock.Click += new EventHandler(btnUnblock_Click);
            
            base.CreateChildControls();
        }

        void btnUnblock_Click(object sender, EventArgs e)
        {
            if (((base.Request != null) && (base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"] != null)) && !string.IsNullOrEmpty(base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"]))
            {
                SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
                Themes themeCollection = webApplication.GetChild<Themes>("ThemeCollection");
                List<string> themes = themeCollection.TemplateIDs;

                string[] ids = base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"].ToString().Split(new Char[] { '#' });

                foreach (string id in ids)
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        themes.Remove(id);
                    }
                }

                themeCollection.TemplateIDs = themes;
                themeCollection.Update();

            }
        }

        void btnBlock_Click(object sender, EventArgs e)
        {
            if (((base.Request != null) && (base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"] != null)) && !string.IsNullOrEmpty(base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"]))
            {
                SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
                Themes themeCollection = webApplication.GetChild<Themes>("ThemeCollection");
                List<string> themes = new List<string>();

                // If no settings previously created, create them now.    
                if (themeCollection == null)
                {
                    SPPersistedObject parent = webApplication;
                    themeCollection = new Themes("ThemeCollection", parent, Guid.NewGuid());
                    themeCollection.Update();
                }

                if (themeCollection.TemplateIDs != null)
                {
                    themes = themeCollection.TemplateIDs;
                }


                string[] ids = base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"].ToString().Split(new Char[] { '#' });

                foreach (string id in ids)
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        themes.Add(id);
                    }
                }

                themeCollection.TemplateIDs = themes;
                themeCollection.Update();
            }

        }

      
        

        public void UnBlockLink(object sender, EventArgs e)
        {
            
        }

        public void BlockLink(object sender, EventArgs e)
        {
          

        }

        private List<string> BlockedThemes(ref List<string> blockedThemes)
        {
            SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
            Themes settings = webApplication.GetChild<Themes>("ThemeCollection");
            if (settings != null)
            {
                if (settings.TemplateIDs != null)
                {
                    blockedThemes = settings.TemplateIDs;
                }
            }

            return blockedThemes;
        }

        private void BindData()
        {
            try
            {
                GetAllThemes(ref themesDataView, ref blockedThemesDataView);

                gridView.DataSource = themesDataView;
                gridView.DataBind();

                gridViewBlocked.DataSource = blockedThemesDataView;
                gridViewBlocked.DataBind();
            }

            catch (Exception error)
            {
                Trace.Write(error.Message.ToString());
            }
        }

        private void GetAllThemes(ref DataView themesView, ref DataView blockedThemesView)
        {

            DataTable allThemesTable = uxWebApplicationSelector.CurrentItem.GetAllThemes().ToTable();

            List<string> blockedThemes = new List<string>();
            BlockedThemes(ref blockedThemes);

            DataTable themesTable = new DataTable();
            DataTable blockedThemesTable = new DataTable();

            themesTable = allThemesTable.Clone();
            blockedThemesTable = allThemesTable.Clone();

            foreach (DataRow row in allThemesTable.Rows)
            {
                bool isBlocked = false;
                foreach (string templateId in blockedThemes)
                {
                    if (row["TemplateID"].ToString() == templateId)
                    {
                        isBlocked = true;
                        break;
                    }
                }

                if (!isBlocked)
                {
                    DataRow themeRow = themesTable.NewRow();
                    themeRow.ItemArray = row.ItemArray;
                    themesTable.Rows.Add(themeRow);
                }
                else
                {
                    DataRow blockedThemeRow = blockedThemesTable.NewRow();
                    blockedThemeRow.ItemArray = row.ItemArray;
                    blockedThemesTable.Rows.Add(blockedThemeRow);
                }
            }

            themesView = new DataView(themesTable);
            blockedThemesView = new DataView(blockedThemesTable);
        }
    }
}
